package onlinebanking.client.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class LoginPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = 1L;

	private JTextField usernameField;

	private JPasswordField passwordField;
	private JButton loginButton;
	private JLabel passwordLabel;
	private JLabel usernameLabel;

	private ClientPanel parent;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new LoginPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public LoginPanel() {
		super();
		this.parent = null;
		initGUI();
	}
	
	public LoginPanel(ClientPanel parent) {
		super();
		this.parent = parent;
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setBackground(new java.awt.Color(190,255,174));
			this.setLayout(null);
			this.setBounds(0, 0, 400, 100);
			this.setPreferredSize(new java.awt.Dimension(475, 35));
			this.setLayout(null);
			this.setSize(500, 35);
			this.setOpaque(false);
			{
				usernameField = new JTextField();
				this.add(usernameField);
				usernameField.setBounds(86, 4, 100, 25);
				usernameField.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						usernameFieldActionPerformed(evt);
					}
				});
			}
			{
				passwordField = new JPasswordField();
				this.add(passwordField);
				passwordField.setBounds(264, 5, 100, 25);
				passwordField.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						passwordFieldActionPerformed(evt);
					}
				});
			}
			{
				usernameLabel = new JLabel();
				this.add(usernameLabel);
				usernameLabel.setText("Username:");
				usernameLabel.setBounds(8, 7, 81, 20);
			}
			{
				passwordLabel = new JLabel();
				this.add(passwordLabel);
				passwordLabel.setText("Password:");
				passwordLabel.setBounds(198, 8, 60, 20);
			}
			{
				loginButton = new JButton();
				this.add(loginButton);
				loginButton.setText("LOGIN");
				loginButton.setBackground(new java.awt.Color(87,223,118));
				loginButton.setBounds(387, 7, 83, 23);
				loginButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						loginButtonActionPerformed(evt);
					}
				});

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * Gets the reference to the username text field. This should be used for
	 * logging in.
	 * 
	 * @return The reference to the username field
	 */
	JTextField getUsernameField() {
		return usernameField;
	}

	/**
	 * Gets the reference to the password field. This should be used for logging
	 * in.
	 * 
	 * @return The reference to the password field.
	 */
	JPasswordField getPasswordField() {
		return passwordField;
	}

	/**
	 * Gets the reference to the login button.
	 * Use this to add the event listeners
	 * 
	 * @return The reference to the login button.
	 */
	JButton getLoginButton() {
		return loginButton;
	}
	
	private void loginButtonActionPerformed(ActionEvent evt) {
		String username = usernameField.getText();
		String password = new String(passwordField.getPassword());
		passwordField.setText("");
		
		if(username == null || password == null) {
			System.out.println("LoginPanel:Incomplete parameters"); //TODO:
			return;
		}
		
		System.out.println("LoginPanel:loginbutton: U:"+username+" p:"+password);
		parent.setUsername("Logged in as "+username);
		parent.getSimpleClient().login(username, password);
	}
	
	private void usernameFieldActionPerformed(ActionEvent evt) {
		loginButtonActionPerformed(evt);
	}
	
	private void passwordFieldActionPerformed(ActionEvent evt) {
		loginButtonActionPerformed(evt);
	}

}
